package com.dengke.hellospringmvc.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;

import com.dengke.hellospringmvc.model.MyUploadForm;

@Controller
public class FileUploadController {

	@InitBinder
	public void initBinder(WebDataBinder dataBinder) {
		Object target=dataBinder.getTarget();
		if (target==null) {
			return;
		}
		System.out.println("Target ="+target);
		if (target.getClass()==MyUploadForm.class) {
			dataBinder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
		}
	}
	
	@RequestMapping(value="/uploadOneFile",method=RequestMethod.GET)
	public String uploadOneFileHandler(Model model) {
		MyUploadForm myUploadForm=new MyUploadForm();
		model.addAttribute("myUploadForm", myUploadForm);
		return "uploadOneFile";
	}
	
	@RequestMapping(value="/uploadOneFile",method=RequestMethod.POST)
	public String uploadOneFileHandlerPost(HttpServletRequest request,
			Model model,@ModelAttribute("myUploadForm") MyUploadForm myUploadForm) {
		return this.doUpload(request, model, myUploadForm);
	}
	
	
	 // GET: Show upload form page.
	  @RequestMapping(value = "/uploadMultiFile", method = RequestMethod.GET)
	  public String uploadMultiFileHandler(Model model) {
	 
	      MyUploadForm myUploadForm = new MyUploadForm();
	      model.addAttribute("myUploadForm", myUploadForm);
	 
	      // Forward to "/WEB-INF/pages/uploadMultiFile.jsp".
	      return "uploadMultiFile";
	  }
	 
	  // POST: Do Upload
	  @RequestMapping(value = "/uploadMultiFile", method = RequestMethod.POST)
	  public String uploadMultiFileHandlerPOST(HttpServletRequest request, //
	          Model model, //
	          @ModelAttribute("myUploadForm") MyUploadForm myUploadForm) {
	 
	      return this.doUpload(request, model, myUploadForm);
	 
	  }
	 
	
	  private String doUpload(HttpServletRequest request,Model model,MyUploadForm myUploadForm) {
		String description=myUploadForm.getDescription();
		System.out.println("Description:"+description);
		//root目录
		String uploadRootPath=request.getServletContext().getRealPath("upload");
		System.out.println("UploadRootPath:"+uploadRootPath);
		File uploadRootDir=new File(uploadRootPath);
		if(!uploadRootDir.exists()){
			uploadRootDir.mkdirs();
		}
		CommonsMultipartFile[]fileDatas=myUploadForm.getFileDatas();
		List<File>uploadFiles=new ArrayList<File>();
		for (CommonsMultipartFile commonsMultipartFile : fileDatas) {
			//cilent file name
			String name=commonsMultipartFile.getOriginalFilename();
			System.out.println("Client File Name:"+name);
			if (name!=null&&name.length()>0) {
				try {
					File severFile=new File(uploadRootDir.getAbsolutePath()+File.separator+name);
					
					BufferedOutputStream stream=new BufferedOutputStream(new FileOutputStream(severFile));
					stream.write(commonsMultipartFile.getBytes());
					stream.close();
					uploadFiles.add(severFile);
					System.out.println("Write file:"+severFile);
					
				} catch (Exception e) {
					System.out.println("Error Write file :"+name);
				}
			}
		}
	      model.addAttribute("description", description);
	      model.addAttribute("uploadedFiles", uploadFiles);
	      return "uploadResult";
	}
}
